package com.zrrd.yt.cart.controller;

import com.zrrd.yt.cart.service.CartInfoService;
import com.zrrd.yt.common.result.Result;
import com.zrrd.yt.common.util.AuthContextHolder;
import com.zrrd.yt.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartInfoController {
    @Autowired
    private CartInfoService cartInfoService;

    //添加购物车
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum,
                            HttpServletRequest request) {
        //获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        System.out.println("购物车服务收到添加请求=====>");
        System.out.println("传递的UserId=====>"+userId);

        cartInfoService.addToCart(skuId, userId, skuNum);
        return Result.ok("添加购物车成功");
    }

    //查询购物车
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request) {
        //获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //调用业务层
        List<CartInfo> cartList = cartInfoService.getCartList(userId, userTempId);
        return Result.ok(cartList);
    }

    //更新选中状态
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checCart(@PathVariable("skuId") Long skuId,
                           @PathVariable("isChecked") Integer isChecked,
                           HttpServletRequest request){
        //获取用户iD
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //调用业务层方法
        cartInfoService.chekCart(userId,skuId,isChecked);
        return Result.ok("更新状态成功");
    }
    
    //删除购物车指定商品
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId,HttpServletRequest request){
        //获取用户iD
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCartOne(userId,skuId);
        return Result.ok("删除成功");
    }

    //获取选中的商品
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") String userId){
        return cartInfoService.getCheckedCartList(userId);
    }

    //更新购物车数据（更新最新价格）
    @GetMapping("/loadCartData/{userId}")
    public Result loadCartData(@PathVariable("userId") String userId){
        cartInfoService.getCartInfoByDB(userId);
        return Result.ok();
    }
}
